package com.cl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cl.pojo.User;
import com.cl.pojo.Vaccinator;
import com.cl.pojo.Vaccines;
import com.cl.service.impl.VaccinatorServiceImpl;
import com.cl.service.impl.VaccinesServiceImpl;
import com.cl.utils.MsgUtil;
import com.cl.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
public class SubscribeController {
    @Autowired
    private VaccinesServiceImpl vaccinesService;
    @Autowired
    private VaccinatorServiceImpl vaccinatorService;
    @Autowired
    private MsgUtil msgUtil;
    @Autowired
    private RedisUtil redisUtil;
    @PostMapping("/getVaccines")
    public String getVaccines(){
        if (redisUtil.lGetListSize("vaccines")==0){
            List<Vaccines> vaccines = vaccinesService.queryAll();
            redisUtil.lSet("vaccines",vaccines);
        }
        return JSON.toJSONString(redisUtil.lGet("vaccines",0,redisUtil.lGetListSize("vaccines")));
    }
    @PostMapping("/submit")
    public String submit(@RequestParam("vaccinator")String vaccinator,@RequestParam("uId")String uId){
        int anInt = Integer.parseInt(uId);
        Vaccinator vaccinator1 = new Vaccinator();
        JSONObject jsonObject = JSON.parseObject(vaccinator);
        vaccinator1.setsAge(jsonObject.get("uAge").toString()+jsonObject.get("age").toString());
        vaccinator1.setsMomname((String) jsonObject.get("momName"));
        vaccinator1.setsAddress((String) jsonObject.get("address"));
        vaccinator1.setsName((String) jsonObject.get("uName"));
        vaccinator1.setsVaccines((String) jsonObject.get("vaccines"));
        vaccinator1.setsSex((String) jsonObject.get("sex"));
        vaccinator1.setsDadname((String) jsonObject.get("dadName"));
        vaccinator1.setsDadphone((String) jsonObject.get("dadPhone"));
        vaccinator1.setsMomphone((String) jsonObject.get("momPhone"));
        vaccinator1.setsAppointmenttime((String) jsonObject.get("time"));
        vaccinator1.setuId(anInt);
        int i = vaccinatorService.insertVaccinator(vaccinator1);
        if (i>0){
            return JSON.toJSONString(msgUtil.success("预约成功"));
        }
        return JSON.toJSONString(msgUtil.fail("预约失败"));
    }
    @PostMapping("/getTime")
    public String getTime(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        int i = user.getuId();
        Vaccinator vaccinator = vaccinatorService.queryByUid(i);
        if (vaccinator!=null){
            return JSON.toJSONStringWithDateFormat(vaccinator,"yyyy-MM-hh", SerializerFeature.WriteDateUseDateFormat);
        }
        return JSON.toJSONString(msgUtil.fail("查询失败"));
    }
    @PostMapping("/modifyTime")
    public String modifyTime(@RequestParam("modifyTime")String modifyTime,@RequestParam("uId")String uId){
        int i = Integer.parseInt(uId);
        int i1 = vaccinatorService.updateTimeByUid(modifyTime, i);
        if (i1>0){
            return JSON.toJSONString(msgUtil.success("修改成功"));
        }
        return JSON.toJSONString(msgUtil.fail("修改失败"));
    }
    @PostMapping("/remove")
    public String remove(@RequestParam("uId")String uId){
        int i = Integer.parseInt(uId);
        int remove = vaccinatorService.remove(i);
        if (remove>0){
            return JSON.toJSONString(msgUtil.success("删除成功"));
        }
        return JSON.toJSONString(msgUtil.fail("删除失败"));
    }
    @GetMapping("/number")
    public String getNumber(){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int[] ints = new int[12];
        int[] month = new int[12];
        for (int i = 1; i <= 12 ; i++) {
            int i1 = vaccinatorService.selectNumber(i,year);
            ints[i-1] = i1;
            month[i-1] = i;
        }
        Map<String, String> map = new HashMap<>();
        map.put("count",Arrays.toString(ints));
        map.put("month",Arrays.toString(month));
        return JSON.toJSONString(map);
    }
}
